package team.umbc;

import android.app.Service;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.IBinder;

public class PlaceService extends Service {
	
	static final int DELAY = 60000;
	private boolean runFlag = false;
	private Updater updater;
	
	PlacesDB places;
	SQLiteDatabase db;
	//for PlaceAdapter
	static final int numPlaces = 5;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	@Override
	public void onCreate(){
		super.onCreate();
		places = new PlacesDB(this);
		this.updater = new Updater();
	}
	
	@Override
	public int onStartCommand(Intent intent, int flag, int startId){
		if(!runFlag){
			this.runFlag = true;
			this.updater.start();
		}
		return Service.START_STICKY;
	}

	@Override
	public void onDestroy(){
		super.onDestroy();
		this.runFlag = false;
		this.updater.interrupt();
		this.updater = null;
	}
	
	private class Updater extends Thread {
		
		public Updater() {
			super("PlaceService-Updater");
		}
		
		@Override
		public void run(){
			PlaceService service = PlaceService.this;
			if(service.runFlag){
				PlaceApplication check = (PlaceApplication) service.getApplication();	
				check.fetchPlaces();
			}
			
		}
	}
}
